// pages/archives/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        childInfo: {},
        child_id: "",
        child: [],
        user_id: "",
        showSelectPopup: false,
        radioId: "",
        i: 1,
        none: false,
        pos: {
            x: 0,
            y: 0
        },
        daInfo: {}, //档案信息对象

    },
   
    /** 切换孩子 */
    changeChildFun() {
        this.setData({
            showSelectPopup: true
        })
    },
    /** 选择孩子触发 */
    onChangeRadio(e) {
        this.setData({
            radioId: e.detail.detail,
        });
    },
    selectChildFun() {
        this.data.child.forEach(ele => {
            if (this.data.radioId == ele.id) {
                wx.setStorageSync('childInfo', ele)
                this.init()
                this.setData({
                    showSelectPopup: false
                })
            }
        })
    },
    /** 切换tabs触发 */
    changeTab(e) {
        this.setData({
            i: e.currentTarget.dataset.tabi,
        })
    },
    selectChildFun2(){
        this.setData({
            showSelectPopup: false
        })
        setTimeout(() => {
            this.init()
        }, 500);
    },

    init() {
        const child = wx.getStorageSync('child')
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            child_id: childInfo.id ? childInfo.id : 0,
            childInfo: childInfo,
            child: child,
            user_id: userInfo.id,
            radioId: childInfo.id
        })
        this.getChildInfo()
        setTimeout(() => {
            var userHouseItem = this.selectComponent('#userMess')
            if (userHouseItem) userHouseItem.getLineData() // 调取子组件方法
        }, 100);
    },
    /** 获取孩子档案信息 */
    getChildInfo() {
        http.getHttp(api.getChildInfo + this.data.child_id, {},
            (result) => {
                if (result.code == 0) {
                    result.data.forEach(ele => {
                        ele.arr.forEach(row => {
                            row.file_id = row.file_id && row.file_id.split(',')
                        })
                    })
                    this.setData({
                        daInfo: result.data,
                        none: true
                    })
                } else if (result.code == -1) {
                    // wx.showToast({
                    //     icon: "success",
                    //     image: '../../images/ok.png',
                    //     title: "暂无档案信息",
                    //     mask: true
                    // })
                    this.setData({
                        none: false
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /** 跳转上传档案页面 */
    toUploadArchivesPage() {
        wx.navigateTo({
            url: '/pages/uploadArchives/index?child_id=' + this.data.child_id + '&mode=add',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    pos: {
                        x: res.windowWidth - 100,
                        y: res.windowHeight - 100
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.init()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})